﻿using System;

namespace Ndns.Packet.ResourceRecords
{
    public sealed class CustomDnsResourceRecord : DnsResourceRecord
    {

        private byte[] _data;

        public override DnsRecordType Type { get; set; }

        public CustomDnsResourceRecord() : base()
        {
            _data = Array.Empty<byte>();
        }
        public CustomDnsResourceRecord(DnsHostName hostName, DnsClass c, DnsRecordType type, uint ttl)
            : base(hostName, c, ttl)
        {
            Type = type;
            _data = Array.Empty<byte>();
        }
        public CustomDnsResourceRecord(DnsHostName hostName, DnsClass c, DnsRecordType type, uint ttl, byte[] data)
            : base(hostName, c, ttl)
        {
            Type = type;
            _data = data ?? throw new ArgumentNullException(nameof(data));
        }


        public override DnsResourceRecord Clone()
        {
            return new CustomDnsResourceRecord(HostName, Class, Type, TTL, _data);
        }

        public override byte[] GetData()
        {
            return _data;
        }
        public void SetData(byte[] data)
        {
            _data = data ?? throw new ArgumentNullException(nameof(data));
        }

        public static CustomDnsResourceRecord OfRecord(DnsResourceRecord record)
        {
            var customRecords = record as CustomDnsResourceRecord;
            if (customRecords == null)
                return new CustomDnsResourceRecord(record.HostName, record.Class, record.Type, record.TTL, record.GetData());
            return customRecords;
        }
    }
}
